package com.szh.zx.javacorecorret.chap1;

//import com.sun.org.apache.xpath.internal.operations.String;

import java.util.concurrent.Semaphore;

// 章节 1.1.4
// 演示中断
public class AcquireUninterruptibly {
	private Semaphore semaphore = new Semaphore(1);

	 void testMethod() {
		try {
			semaphore.acquire();
			System.out.println(Thread.currentThread().getName() + "：begin time:" + System.currentTimeMillis());
			for (int i = 0; i < Integer.MAX_VALUE / 50; i++) {
				String newString = new String();
				Math.random();
			}
			System.out.println(Thread.currentThread().getName() + "：end time:" + System.currentTimeMillis());
			semaphore.release();
		} catch (InterruptedException e) {

			System.out.println("线程："+Thread.currentThread().getName()+"进入了 cache");
			e.printStackTrace();
		}
	}


	public static void main(String[] args) throws InterruptedException {
		AcquireUninterruptibly service = new AcquireUninterruptibly();

		ThreadE threadE = new ThreadE(service);
		ThreadF threadF = new ThreadF(service);
		threadE.setName("E");
		threadF.setName("F");
		threadE.start();
		threadF.start();

		Thread.sleep(1000);
		threadE.interrupt();
		System.out.println("man 中断了 E");
	}
}
class ThreadE extends Thread{
	private AcquireUninterruptibly service;

	public ThreadE(AcquireUninterruptibly service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.testMethod();
	}
}
class ThreadF extends Thread{
	private AcquireUninterruptibly service;

	public ThreadF(AcquireUninterruptibly service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.testMethod();
	}
}
